<script>
export default {
  name: "LoginView",
  data() {
    return {
      tabSelected: 0,
      tabList: ["密码登录", "免密码登录"],
      userForm: {
        username: "",
        password: ""
      },
      userRules: {
        username: [
          {required: true, message: "请输入用户名或手机号", trigger: "blur"}
        ],
        password: [
          {required: true, message: "请输入密码", trigger: "blur"}
        ]
      },
      smsForm: {
        phone: "",
        code: ""
      },
      smsRules: {
        phone: [
          {required: true, message: "请输入手机号", trigger: "blur"},
          {pattern: /^1[3456789]\d{9}$/, message: '手机号格式错误', trigger: 'blur'},
        ],
        code: [
          {required: true, message: "请输入验证码", trigger: "blur"}
        ]
      },
      btnSmsDisabled: false,
      btnSmsText: "发送验证码"
    }
  },
  methods: {
    changeTab(idx) {
      this.tabSelected = idx
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$router.push({path: "/"})
      })
    },
    sendSmsCode() {
      this.$refs.smsForm.validateField("phone", (error) => {
        if (error) {
          return false;
        }
        // 验证通过
        // 1. 禁用按钮
        this.btnSmsDisabled = true;
        // 2. 按钮显示倒计时
        let count_down = 60;
        let Interval = window.setInterval(() => {
          count_down -= 1;
          this.btnSmsText = `${count_down}秒后重发`;
          if (count_down < 1) {
            // 3. 倒计时完成恢复按钮
            this.btnSmsText = "重新发送";
            this.btnSmsDisabled = false;
            window.clearInterval(Interval);
          }
        }, 1000)
      })
    }
  }
}
</script>

<template>
  <div class="main">
    <div class="loginBox">
      <div class="tabBoxSwitch">
        <ul class="tabBoxSwitchUl">
          <li :class="tabSelected===index?'tab-active':''" v-for="(val,index) in tabList" :key="index"
              @click="changeTab(index)">{{ val }}
          </li>
        </ul>
      </div>

      <div class="tabBoxInput">
        <div v-show="tabSelected===0">
          <el-form :model="userForm" :rules="userRules" ref="userForm">
            <el-form-item prop="username" style="margin-top: 20px">
              <el-input v-model="userForm.username" placeholder="用户名或手机号"></el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input v-model="userForm.password" placeholder="密码" show-password></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="submitForm('userForm')">登录</el-button>
            </el-form-item>
          </el-form>
        </div>

        <div v-show="tabSelected===1">
          <el-form :model="smsForm" :rules="smsRules" ref="smsForm">
            <el-form-item prop="phone" style="margin-top: 20px">
              <el-input v-model="smsForm.phone" placeholder="手机号"></el-input>
            </el-form-item>
            <el-form-item prop="code">
              <el-row type="flex" justify="space-between">
                <el-input v-model="smsForm.code" placeholder="验证码"></el-input>
                <el-button @click="sendSmsCode" :disabled="btnSmsDisabled" style="margin-left: 10px">
                  {{ btnSmsText }}
                </el-button>
              </el-row>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="submitForm('smsForm')">登录</el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.main {
  width: 100%;
  height: 100vh;
  background-color: #2E3E9C;

  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.loginBox {
  background-color: #FFFFFF;
  box-shadow: 0 1px 3px rgba(26, 26, 26, 0.1);
  border-radius: 2px;
  width: 380px;
  min-height: 200px;
  padding: 0 24px 20px;
}

.tabBoxSwitch .tabBoxSwitchUl {
  list-style: none;
  padding: 0;
  margin: 0;
}

.tabBoxSwitch .tabBoxSwitchUl li {
  display: inline-block;
  height: 60px;
  font-size: 16px;
  line-height: 60px;
  margin-right: 24px;
  cursor: pointer;
}

.tab-active {
  position: relative;
  color: #1a1a1a;
  font-weight: 600;
  font-synthesis: style;
}


.tab-active::before {
  display: block;
  position: absolute;
  bottom: 0;
  content: "";
  width: 100%;
  height: 3px;
  background-color: #0084ff;
}
</style>